/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.trace;

import java.util.Map;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jamonapi.Monitor;
import com.jamonapi.MonitorFactory;

/**
 * Collect performance stats for the JSF lifecycle.
 * 
 * @author boudyacho
 * 
 */
public class JamonPhaseListener implements PhaseListener {

	private static final long serialVersionUID = 1L;

	private static final Log logger = LogFactory
			.getLog(JamonPhaseListener.class);

	private static final String PREFIX = "Jamon:";

	public PhaseId getPhaseId() {
		return PhaseId.ANY_PHASE;
	}

	public void beforePhase(PhaseEvent e) {
		if (logger.isDebugEnabled()) {
			if (PhaseId.RESTORE_VIEW.equals(e.getPhaseId())) {
				Monitor monitor = getRequestMonitor(e);
				monitor.start();
			}
			Monitor monitor = getPhaseMonitor(e);
			monitor.start();
		}
	}

	public void afterPhase(PhaseEvent e) {
		if (logger.isDebugEnabled()) {
			Monitor monitor = getPhaseMonitor(e);
			monitor.stop();

			if (PhaseId.RENDER_RESPONSE.equals(e.getPhaseId())) {
				monitor = getRequestMonitor(e);
				monitor.stop();
			}
		}
	}

	protected Monitor getRequestMonitor(PhaseEvent e) {
		return getMonitor(e, getUri(e));
	}

	protected Monitor getPhaseMonitor(PhaseEvent e) {
		return getMonitor(e, getKey(e));
	}

	@SuppressWarnings("unchecked")
	protected Monitor getMonitor(PhaseEvent e, String label) {
		Map requestMap = e.getFacesContext().getExternalContext()
				.getRequestMap();
		Monitor monitor = (Monitor) requestMap.get(PREFIX + label);

		if (monitor == null) {
			monitor = MonitorFactory.start(label);
			requestMap.put(PREFIX + label, monitor);
		}

		return monitor;
	}

	protected String getKey(PhaseEvent e) {
		String uri = getUri(e);
		PhaseId phaseId = e.getPhaseId();
		return uri + ":" + phaseId;
	}

	protected String getUri(PhaseEvent e) {
		HttpServletRequest request = (HttpServletRequest) e.getFacesContext()
				.getExternalContext().getRequest();
		return request.getRequestURI();
	}
}
